822B - Crossword solving - CodeForces Solution


brute force implementation strings *1000

Please click on ads to support us..

Python Code:

n, m = map(int, input().split())
s = input()
t = input()

k = -1
indices = []

for i in range(m-n+1):
    diferente = 0
    listaDifs = []
    for j in range(n):
        if t[i+j] != s[j]:
            diferente += 1
            listaDifs.append(j+1)
    if k == -1 or diferente < k:
        k = diferente
        indices = listaDifs

print(k)

if k > 0:
    print(*indices)
 							 	     		 			 	   		

C++ Code:

#include <bits/stdc++.h>
using namespace std;

int main() {
	// your code goes here
	int n,m;
	cin>>n>>m;
	string str1,str2;
	cin>>str1>>str2;
	int mini=INT_MAX;
	vector<int>v;
	bool ans=false;
	for(int i=0;i<=m-n;i++){
	    int ct=0;
	    vector<int>res;
	    for(int j=i;j<i+n;j++){
	        if(str2[j]!=str1[j-i]){
	            ct++;
	            res.push_back(j-i+1);
	        }
	    }
	    if(ct==0){
	        ans=true;
	        break;
	    }
	    if(ct<mini){
	        v.clear();
	        mini=ct;
	        for(auto ele:res){
	            v.push_back(ele);
	        }
	    }
	}
	if(ans){
	    cout<<0<<endl;
	}
	else{
	cout<<mini<<endl;
	for(auto ele:v){
	    cout<<ele<<" ";
	}cout<<endl;
	}
	return 0;
}


Comments

Submit
0 Comments
More Questions

115A - Party
746B - Decoding
1424G - Years
1663A - Who Tested
1073B - Vasya and Books
195B - After Training
455A - Boredom
1099A - Snowball
1651D - Nearest Excluded Points
599A - Patrick and Shopping
237A - Free Cash
1615B - And It's Non-Zero
1619E - MEX and Increments
34B - Sale
1436A - Reorder
1363C - Game On Leaves
1373C - Pluses and Minuses
1173B - Nauuo and Chess
318B - Strings of Power
1625A - Ancient Civilization
864A - Fair Game
1663B - Mike's Sequence
448A - Rewards
1622A - Construct a Rectangle
1620A - Equal or Not Equal
1517A - Sum of 2050
620A - Professor GukiZ's Robot
1342A - Road To Zero
1520A - Do Not Be Distracted
352A - Jeff and Digits